package com.example.wechat.wechat;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.*;
import java.util.List;

public class ChatItemAdapter extends RecyclerView.Adapter<ChatItemAdapter.ViewHolder> {

    private List<ChatItem> mChatItemList;
    private Activity mActivity;
    static class ViewHolder extends RecyclerView.ViewHolder{
        //private  List<ChatItem> mChatItemList;
        LinearLayout linearLayout;//点击一个Item时需要定义recycleView中的一个item
        ImageView chatItemImage;
        TextView chatItemTitle;
        TextView chatItemContent;

        public ViewHolder(View view){
            super(view);
            linearLayout = view.findViewById(R.id.chatItem);
            chatItemImage = (ImageView) view.findViewById(R.id.chatItem_image);
            chatItemTitle = (TextView) view.findViewById(R.id.chatItem_title);
            chatItemContent = (TextView) view.findViewById(R.id.chatItem_content);
        }
    }

    public ChatItemAdapter( Activity activity,List<ChatItem> chatItemList){
        mActivity = activity;
        mChatItemList = chatItemList;
    }


    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chatitem, viewGroup ,false);
        final ViewHolder holder = new ViewHolder(view);

        //点击单个Item跳到其聊天界面
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.startActivity(new Intent(mActivity, Chat.class));
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ChatItem chatItem = mChatItemList.get(position);
        holder.chatItemImage.setImageResource(chatItem.getImageId());
        holder.chatItemTitle.setText(chatItem.getTitle());
        holder.chatItemContent.setText(chatItem.getContent());

    }


        public int getItemCount(){
            return mChatItemList.size();
        }





}
